<?php
namespace app\index\controller;
use think\Controller;
use app\index\controller\Base;
use think\Db;
use think\Request;
class Artice extends Base
{
	//判断是否是POST请求
	public function ispost(){
		if (!Request::instance()->isPost()){
			return $this->error('非法请求');
		}
	}
    public function index()
    {
    	$base = new Base();
    	$base->bloginfo();
    	$id = input('id');
    	if (!is_numeric($id)) {
    		$this->error();
    	}
    	$artice = db('blog')
    	->where('id', '=', $id)
    	->where('show', '=', 1)
    	->find();
    	$cat = db('category')->where('id', '=', $artice['cid'])->find();
    	//下一条文章数据
    	$downartice = db('blog')
    	->where('id', '>', $id)
    	->where('type', '=', 'artice')
    	->where('show', '=', 1)
    	->find();
    	//上一条文章数据
    	$upartice = db('blog')
    	->where('id', '<', $id)
    	->where('type', '=', 'artice')
    	->where('show', '=', 1)
    	->find();
    	if (empty($downartice)) {
    		$downartice['id']=$id;
    		$downartice['title']="没有更多了,到底啦";
    	}
    	if (empty($upartice)) {
    		$upartice['id']=$id;
    		$upartice['title']="没有更多了,到底啦";
    	}
        //加载评论
        $comment = db('comment')->order('time','dec')->where('ac_id', '=', $id)->select();
        if (empty($artice)) {
            $this->error();
        }
        //阅读数量+1
        db('blog')->where('id',$id)->setInc('click');
    	return $this->fetch('./index/artice',[
    		'artice'=>$artice,
    		'cat'=>$cat,
    		'downartice'=>$downartice,
    		'upartice'=>$upartice,
            'comment'=>$comment
    		]);
    }
    public function acgood(){
    	$id = input('id');
    	$acgood = db('blog')->where('id', '=', $id)->setInc('zan');
    	if ($acgood) {
			$data = ["result"=>1];
			echo json_encode($data);
		}else{
			$data = ["result"=>2];
			echo json_encode($data);
		}
    }

    //获取真实ip
    public function ip() {
        if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $ip = getenv('REMOTE_ADDR');
        } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
    }
    //文章评论处理
    public function content(){
    	$this->isPost();
    	$id = input('id');
        // 设置cookie三年
        cookie('visitorname', input('name'), 93312000);
        cookie('visitorurl', input('url'), 93312000);
        cookie('visitoremail', input('email'), 93312000);
    	$data = [
    		'ac_id' => $id,
    		'name'	=> input('name'),
    		'time'	=> time(),
    		'mail'	=> input('email'),
    		'text'	=> input('content'),
            'url'  => input('url'),
    		'ip'	=> $this->ip(),
            'onemail' => input('onemail','0'),
            'com_id' => input('com_id','0'),
            '__token__'=> input('__token__'),

    	];
        $validate = new \app\index\validate\reply;
        if (!$validate->check($data)) {
            $data = ["result"=>2, "msg"=>"请不要尝试重复提交"];
            echo json_encode($data);
        }else{
            if ($data['com_id']==0) {
                //没有@任何人
                Db::name('comment')->strict(false)->insert($data);
            }else{
                //@某人，判断时候需要发送邮件
                $at = Db::name('comment')->where('uid',$data['com_id'])->find();
                if (!empty($at)) {
                    if ($at['onemail']=='on') {
                        $weburl = $_SERVER['HTTP_REFERER'];
                        $con = "亲，你的回复有了新回复，详细请访问<br>".$weburl;
                        sendEmail([['user_email'=>$at['mail'],'content'=>$con]]);
                        Db::name('comment')->strict(false)->insert($data);
                    }else{
                        Db::name('comment')->strict(false)->insert($data);
                    }
                }else{
                    Db::name('comment')->strict(false)->insert($data);
                }
            }
            
            $data = ["result"=>1, "msg"=>"评论成功"];
            echo json_encode($data);
        }
		
    }

}
